﻿@page "/tests/datagrid/data/in-memory"
<Row>
    <Column>
        <Card Margin="Margin.Is4.OnY">
            <CardHeader>
                <CardTitle>Datagrid: In Memory</CardTitle>
            </CardHeader>
            <CardBody>
                <Paragraph>
                    Our DataGrid allows you to display, manage, and manipulate tabular data with absolute ease. You can use
                    the <code>Data</code> property to bind the grid to a strongly typed collection. Initialize this collection
                    in the <code>OnInitialized</code> or <code>OnInitializedAsync</code> lifecycle method or before this method
                    is invoked. To display data within our DataGrid, simply add columns to the markup and use the Field
                    property to assign data fields as needed.
                </Paragraph>
            </CardBody>
            <CardBody>
                <DataGrid TItem="Employee"
                          Data="inMemoryData"
                          ShowPager="true"
                          ShowPageSizes="true">
                    <DataGridColumns>
                        <DataGridColumn TextAlignment="TextAlignment.Center" TItem="Employee" Field="@nameof( Employee.Id )" Caption="#" Width="60px" />
                        <DataGridColumn TItem="Employee" Field="@nameof( Employee.FirstName )" Caption="First Name">
                        </DataGridColumn>
                        <DataGridColumn TItem="Employee" Field="@nameof( Employee.LastName )" Caption="Last Name" />
                        <DataGridColumn TItem="Employee" Field="@nameof( Employee.Email )" Caption="Email" />
                        <DataGridColumn TItem="Employee" Field="@nameof( Employee.City )" Caption="City">
                            <CaptionTemplate>
                                <Icon Name="IconName.City" /> @context.Caption
                            </CaptionTemplate>
                        </DataGridColumn>
                        <DataGridColumn TItem="Employee" Field="@nameof( Employee.Zip )" Caption="Zip">
                        </DataGridColumn>
                        <DataGridDateColumn TItem="Employee" Field="@nameof( Employee.DateOfBirth )" DisplayFormat="{0:dd.MM.yyyy}" Caption="Date Of Birth" Editable="true" />
                        <DataGridNumericColumn TItem="Employee" Field="@nameof( Employee.Childrens )" Caption="Childrens" Editable="true" Filterable="false" />
                        <DataGridSelectColumn TItem="Employee" Field="@nameof( Employee.Gender )" Caption="Gender" Editable="true">
                            <DisplayTemplate>
                                @{
                                    var gender = ( context as Employee )?.Gender;

                                    @if ( gender == "M" )
                                    {
                                        @("Male")
                                    }
                                    else if ( gender == "F" )
                                    {
                                        @("Female")
                                    }
                                    else if ( gender == "D" )
                                    {
                                        @("Diverse")
                                    }
                                }
                            </DisplayTemplate>
                        </DataGridSelectColumn>
                        <DataGridColumn TItem="Employee" Field="@nameof( Employee.Salary )" Caption="Salary" Editable="true" Width="140px" DisplayFormat="{0:C}" DisplayFormatProvider="@System.Globalization.CultureInfo.GetCultureInfo("fr-FR")" TextAlignment="TextAlignment.End">
                        </DataGridColumn>
                        <DataGridCheckColumn TItem="Employee" Field="@nameof(Employee.IsActive)" Caption="Active" Editable="true" Filterable="false">
                            <DisplayTemplate>
                                <Check TValue="bool" Checked="context.IsActive" Disabled="true" ReadOnly="true" />
                            </DisplayTemplate>
                        </DataGridCheckColumn>
                    </DataGridColumns>
                </DataGrid>
            </CardBody>
        </Card>
    </Column>
</Row>

@code {

    [Inject] EmployeeData EmployeeData { get; set; }

    private IEnumerable<Employee> inMemoryData;

    protected override async Task OnInitializedAsync()
    {
        inMemoryData = ( await EmployeeData.GetDataAsync().ConfigureAwait( false ) ).Take( 25 );
        await base.OnInitializedAsync();
    }
}